package com.pro.meeting.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pro.meeting.bean.MeetingPub;
import com.pro.meeting.dao.MeetingPubDao;
import com.pro.meeting.service.MeetingPubService;

@Service
public class MeetingPubServiceImpl implements MeetingPubService {
	@Autowired
	MeetingPubDao meetingPubDao;
	
	@Override
	public int add(MeetingPub pub) {

		pub.setPid(UUID.randomUUID().toString());
		pub.setCurrtDate(new Date());
		//会议编号生成方式：
		pub.setPcode(genCode(pub.getPubtime()));
		return meetingPubDao.add(pub);
	}
	/*
	 会议编号生成规则：
	 	根据会议召开时间，
	 	例如：会议召开时间为：2018-02-05-11:30 
	 	   如果当天（会议召开时间）第一单时，该编号为：20180205001
	 	                                                   第二单时，该编号为：20180205002
	 	                 .......
	 */
	public String genCode(String pubTime){
		String pcode="";//生成的会议编号
		
		// 2018-02-06T11:30
		String pTime=pubTime.substring(0,10).replaceAll("-","");//20180206
		//查询会议召开日期  当天的时间前面有几单
//		int num=getPubCountByPubTime(pTime); //查询数量
//		if(num==0){
//			pcode=pTime+"001";
//		}else{ //7
//			pcode=pTime+"001";
//			pcode=(Long.parseLong(pcode)+num)+"";
//		}
		
		//根据召开时间，查询当天最大的会议编号
		String maxCode=getMaxPcodeByPubTime(pTime);
		if(StringUtils.isEmpty(maxCode)){ //空的时候
			pcode=pTime+"001";
		}else{
			pcode=(Long.parseLong(maxCode)+1)+"";
		}
		return pcode;
	}
	
	//根据当天召开日期查询当天前面有几单
	@Override
	public int getPubCountByPubTime(String pTime) {
		// TODO Auto-generated method stub
		return meetingPubDao.getPubCountByPubTime(pTime);
	}
	@Override
	public String getMaxPcodeByPubTime(String pTime) {
		// TODO Auto-generated method stub
		return meetingPubDao.getMaxPcodeByPubTime(pTime);
	}
	@Override
	public List<MeetingPub> getByUid(String uid) {
		// TODO Auto-generated method stub
		return meetingPubDao.getByUid(uid);
	}
	@Override
	public MeetingPub getByPid(String pid) {
		// TODO Auto-generated method stub
		return meetingPubDao.getByPid(pid);
	}
	@Override
	public List<MeetingPub> getMyGrabByUid(MeetingPub meetingPub) {
		// TODO Auto-generated method stub
		return meetingPubDao.getMyGrabByUid(meetingPub);
	}
	//我的抢单列表
	@Override
	public List<MeetingPub> getMyGrabListByUid(String uid) {
		// TODO Auto-generated method stub
		return meetingPubDao.getMyGrabListByUid(uid);
	}

}
